{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "descriptive_statistics.ipynb",
      "provenance": [],
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/towardsai/tutorials/blob/master/descriptive-statistics/descriptive_statistics.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "IfaFN9sNJI_t"
      },
      "source": [
        "#Descriptive Statistics for Data-driven Decision Making with Python\r\n",
        "\r\n",
        "This Jupyter notebook accompanies our book on descriptive statistics.\r\n",
        "\r\n",
        "* Support us by buying our book: https://news.towardsai.net/descriptive-statistics\r\n",
        "* Github repository: https://github.com/towardsai/tutorials/tree/master/descriptive-statistics\r\n",
        "\r\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "nai5znkElMP-",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "174f54f0-722f-400f-83d3-e9f97875fb06"
      },
      "source": [
        "#Calculating the Mean (Page 33):\n",
        "\n",
        "#Import required libraries:\n",
        "from statistics import mean\n",
        "\n",
        "#Data:\n",
        "X = [5,8,15,18,25]\n",
        "\n",
        "#Finding mean:\n",
        "print(\"Mean = \",mean(X))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Mean =  14.2\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "jR0Jpz05lUC8",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "3b85bc6a-05a6-4a93-8b68-c24794af5db4"
      },
      "source": [
        "#Calculating the Weighted Mean (Page 34):\n",
        "\n",
        "#Import required libraries:\n",
        "import  numpy as np\n",
        "\n",
        "#Data:\n",
        "X = [5,10,15,20,25]\n",
        "\n",
        "#Weight:\n",
        "Y = [1,2,2,1,1]\n",
        "\n",
        "#Finding weighted mean:\n",
        "print(\"Weighted Mean = \",np.average(X,weights=Y))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Weighted Mean =  14.285714285714286\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "F2gOlmE6lWno",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "dc5f6cb0-93f9-4680-ddc3-d93a66debbb3"
      },
      "source": [
        "#Calculating the Geometric Mean (Page 36):\n",
        "\n",
        "#Import required libraries:\n",
        "from scipy import stats\n",
        "\n",
        "#Data:\n",
        "X = [20,30,40]\n",
        "\n",
        "#Finding Geometric Mean:\n",
        "print(\"Geometric Mean =\",stats.gmean(X))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Geometric Mean = 28.844991406148168\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "MNu3M61OlZr4",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "b5fb68df-bc34-4a3f-db59-0aa83aaf6119"
      },
      "source": [
        "#Calculating the Harmonic Mean (Page 37):\n",
        "\n",
        "#Import required libraries:\n",
        "from statistics import harmonic_mean\n",
        "\n",
        "#Data:\n",
        "X = [20,30,15]\n",
        "\n",
        "#Finding harmonic mean:\n",
        "print(\"Harmonic Mean =\",harmonic_mean([20,30,15]))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Harmonic Mean = 20.0\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "7kj2STfdlbGg",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "6b9a2ebf-43c9-4102-f450-c9afff322994"
      },
      "source": [
        "#Median (Page 39):\n",
        "#Odd number of observations:\n",
        "\n",
        "#Import required libraries:\n",
        "from statistics import median\n",
        "\n",
        "#Data:\n",
        "X =[3,4,11,56,93]\n",
        "\n",
        "#Finding median:\n",
        "print(\"Median =\",median(X))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Median = 11\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "cIRQ6pODlcII",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "9aa9be80-d7e9-4788-a885-2f418b53ff9b"
      },
      "source": [
        "#Median (Page 40):\n",
        "#Even number of observations:\n",
        "\n",
        "#Import required libraries:\n",
        "from statistics import median\n",
        "\n",
        "#Data:\n",
        "X = [3,4,11,56,93,100]\n",
        "\n",
        "#Finding median:\n",
        "print(\"Median =\",median(X))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Median = 33.5\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "xvSh1k-wlewJ",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "0abd98a8-be2d-4028-85af-2395db5501d6"
      },
      "source": [
        "#Mode (Page 43):\n",
        "\n",
        "#Import required libraries:\n",
        "from statistics import mode\n",
        "\n",
        "#Data:\n",
        "X = [2,7,18,32,32,45,78]\n",
        "\n",
        "#We have to use try-except in case there is no mode or more than one modes:\n",
        "try:\n",
        "    print(\"Mode =\",mode(X))\n",
        "except:\n",
        "    print(\"Mode not found!\")"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Mode = 32\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "R8nGmJg_lfth",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "68da4365-1e3e-4f26-f185-52c1d50e111c"
      },
      "source": [
        "#Mode (Page 44):\n",
        "\n",
        "#Import required libraries:\n",
        "from statistics import mode\n",
        "\n",
        "#Data:\n",
        "X = [2,7,18,32,46,54]\n",
        "\n",
        "#We have to use try-except in case there is no mode or more than one modes:\n",
        "try:\n",
        "    print(\"Mode =\",mode(X))\n",
        "except:\n",
        "    print(\"Mode not found!\")\n",
        "    \n",
        "#An error is thrown when there are no mode."
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Mode not found!\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "sSB62a_VlgfQ",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "deee4da2-6e65-4701-fb74-2e65ceda3ea0"
      },
      "source": [
        "#Mode (Page 45):\n",
        "\n",
        "#Import required libraries:\n",
        "from statistics import mode\n",
        "\n",
        "#Data:\n",
        "X = [2,7,18,18,34,57,57,89]\n",
        "\n",
        "#We have to use try-except in case there is no mode or more than one modes:\n",
        "try:\n",
        "    print(\"Mode =\",mode(X))\n",
        "except:\n",
        "    print(\"Mode not found!\")\n",
        "      \n",
        "#An error is thrown in there are more than one mode."
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Mode not found!\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "-_XfyjDGlhYB",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "3520a91b-0e7a-4ed7-b9c5-ec48fdfa3c05"
      },
      "source": [
        "#Quantiles (Page 52):\n",
        "#Odd number of observations:\n",
        "\n",
        "#Import required libraries:\n",
        "import numpy as np\n",
        "X = [2,3,7,11,29,56,57,78,82,89,95]\n",
        "\n",
        "#Print the values:\n",
        "print(\"Min =\",np.quantile(X,0))\n",
        "print(\"Q1 =\",np.quantile(X,0.25))\n",
        "print(\"Q2 =\",np.quantile(X,0.50))\n",
        "print(\"Q3 =\",np.quantile(X,0.75))\n",
        "print(\"Max =\",np.quantile(X,1))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Min = 2\n",
            "Q1 = 9.0\n",
            "Q2 = 56.0\n",
            "Q3 = 80.0\n",
            "Max = 95\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "fIbYgWDqliUF",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "931a3f63-35cb-419d-8f58-325b151a832e"
      },
      "source": [
        "#Quantiles (Page 52):\n",
        "#Even number of observations:\n",
        "\n",
        "#Import required libraries:\n",
        "import numpy as np\n",
        "X = [2,3,7,11,29,56,57,78,82,89]\n",
        "\n",
        "#Print the values:\n",
        "print(\"Min =\",np.quantile(X,0))\n",
        "print(\"Q1 =\",np.quantile(X,0.25))\n",
        "print(\"Q2 =\",np.quantile(X,0.50))\n",
        "print(\"Q3 =\",np.quantile(X,0.75))\n",
        "print(\"Max =\",np.quantile(X,1))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Min = 2\n",
            "Q1 = 8.0\n",
            "Q2 = 42.5\n",
            "Q3 = 72.75\n",
            "Max = 89\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "_RqjAwLGljLY",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "83c411ca-ac00-4d60-b927-8088f9964322"
      },
      "source": [
        "#Import required libraries (Page 55):\n",
        "from scipy import stats\n",
        "\n",
        "#Data:\n",
        "X= [1,2,3,4,5,6,7,8,9,10]\n",
        "\n",
        "#Find the percentile rank:\n",
        "print(\"Percentile Rank for 7 =\",stats.percentileofscore(X,7,kind=\"strict\"))\n",
        "print(\"Percentile Rank for 9 =\",stats.percentileofscore(X,9,kind=\"strict\"))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Percentile Rank for 7 = 60.0\n",
            "Percentile Rank for 9 = 80.0\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "sPTSMrWZlkEA",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "7f0ec256-135e-4184-cd98-ce68a9019a23"
      },
      "source": [
        "#Import required libraries (Page 55):\n",
        "from scipy import stats\n",
        "\n",
        "#Data:\n",
        "X= [1,2,2,3,4,5,5,6,7,8]\n",
        "\n",
        "#Find the percentile rank:\n",
        "print(\"Percentile Rank for 5 =\",stats.percentileofscore(X,5,kind=\"strict\"))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Percentile Rank for 5 = 50.0\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "5N8KoMRNlk3x",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "77731bda-15c3-4170-ddf6-55304e34308b"
      },
      "source": [
        "#Percentile values (Page 57):\n",
        "\n",
        "#Import required libraries:\n",
        "import numpy as np\n",
        "\n",
        "#Data:\n",
        "X = [2,5,6,8,11,45,64,71,77,89,93]\n",
        "\n",
        "#Print the values:\n",
        "print(\"75th =\",np.percentile(X,75))\n",
        "print(\"70th =\",np.percentile(X,70))\n",
        "print(\"40th =\",np.percentile(X,40))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "75th = 74.0\n",
            "70th = 71.0\n",
            "40th = 11.0\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "fJF_VQVIllvg",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "ca5feaa7-60dc-4be4-93c4-6653154c473e"
      },
      "source": [
        "#Percentile values (Page 59):\n",
        "\n",
        "#Import required libraries:\n",
        "import numpy as np\n",
        "\n",
        "#Data:\n",
        "X = [2,5,6,8,11,45,64,71,77,89,93]\n",
        "\n",
        "#Print the values:\n",
        "print(\"10th =\",np.percentile(X,10))\n",
        "print(\"25th =\",np.percentile(X,25))\n",
        "print(\"75th =\",np.percentile(X,75))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "10th = 5.0\n",
            "25th = 7.0\n",
            "75th = 74.0\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "1Ll88k5nlmjv",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "87f16ece-e809-4b95-fbda-8d3cc0811329"
      },
      "source": [
        "#Range (Page 61):\n",
        "\n",
        "#Data:\n",
        "X = [2,5,6,8,11,15,19,21,25,28,31]\n",
        "\n",
        "#Print values:\n",
        "print(\"Range =\",(max(X)-min(X)))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Range = 29\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "vsWADGN0lnll",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "8e44c287-a86b-4927-fa81-6c6f72f10e39"
      },
      "source": [
        "#Inter Quartile Range(IQR) (Page 62): \n",
        "\n",
        "#Import required libraries:\n",
        "from scipy import stats\n",
        "\n",
        "#Data:\n",
        "X = [2,5,6,8,11,15,19,21,25,28,31]\n",
        "\n",
        "#Interquartile Range:\n",
        "print(\"Interquartile Range =\",stats.iqr(X,interpolation=\"nearest\"))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Interquartile Range = 19\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "_sc6pVWnlo3Y",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "c08996ab-4d16-490d-dca0-bb55d2ba16d8"
      },
      "source": [
        "#Inter Quartile Range(IQR) (Page 64):\n",
        "\n",
        "#Import required libraries:\n",
        "from scipy import stats\n",
        "\n",
        "#Data:\n",
        "X = [2,5,6,8,11,15,19,21,25,28,500]\n",
        "\n",
        "#Range:\n",
        "print(\"Range =\",(max(X)-min(X)))\n",
        "\n",
        "#Interquartile Range:\n",
        "print(\"Interquartile Range =\",stats.iqr(X,interpolation=\"nearest\"))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Range = 498\n",
            "Interquartile Range = 19\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "A0FGIY0-lvSx",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "241e7ea6-794f-41f0-8afb-0b4dcbe8a64b"
      },
      "source": [
        "#Sample variance (Page 68):\n",
        "\n",
        "#Import required libraries:\n",
        "from statistics import variance\n",
        "\n",
        "#Data:\n",
        "X = [20,30,25,50,70,45,75]\n",
        "\n",
        "#Print variance:\n",
        "print(\"Variance =\",variance(X))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Variance = 466.6666666666667\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "gWGNtot4lwV2",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "017cf19e-5e62-45bd-8fdf-4a8cca0f9f90"
      },
      "source": [
        "#Sample Standard Deviation (Page 68):\n",
        "\n",
        "#Import required libraries:\n",
        "from statistics import stdev\n",
        "\n",
        "#Data:\n",
        "X = [20,30,25,50,70,45,75]\n",
        "\n",
        "#Print Standard Deviation:\n",
        "print(\"Standard Deviation =\",stdev(X))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Standard Deviation = 21.602468994692867\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "85OQLR2Gl43C",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "b54b4834-9267-4f1f-9636-edb6898a95b3"
      },
      "source": [
        "#Population variance:\n",
        "\n",
        "#Import required libraries:\n",
        "from statistics import pvariance\n",
        "\n",
        "#Data:\n",
        "X = [20,30,25,50,70,45,75]\n",
        "\n",
        "#Print variance:\n",
        "print(\"Variance =\",pvariance(X))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Variance = 400\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "mQHftIall5uw",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "655fb9ed-b177-48c0-a64f-cd4c52abb59a"
      },
      "source": [
        "#Population Standard Deviation (Page 68):\n",
        "\n",
        "#Import required libraries:\n",
        "from statistics import pstdev\n",
        "\n",
        "#Data:\n",
        "X = [20,30,25,50,70,45,75]\n",
        "\n",
        "#Print Standard Deviation:\n",
        "print(\"Standard Deviation =\",pstdev(X))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Standard Deviation = 20.0\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "bMGFqwfCl6oD",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "9317207c-f320-48e6-e44a-6294ef2a9b1d"
      },
      "source": [
        "#Mean Absolute Deviation (Page 74):\n",
        "\n",
        "#Import required libraries:\n",
        "from numpy import mean, absolute\n",
        "\n",
        "#Data:\n",
        "X = [18,20,34]\n",
        "\n",
        "#Print Mean Absolute Deviation:\n",
        "print(\"Mean Absolute Deviation = \",mean(absolute(X - mean(X))) )"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Mean Absolute Deviation =  6.666666666666667\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ZAAs3T8Wl7j3",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "c2d6bf55-4a2d-4edd-c93c-5579cc44e2a5"
      },
      "source": [
        "#Coefficient of variation (Page 78):\n",
        "\n",
        "#Import required libraries:\n",
        "from statistics import mean,stdev\n",
        "\n",
        "#Data:\n",
        "X = [51710,51350,51400,51450,51460]\n",
        "Y = [62.009,61.962,62.797,61.996,63.158]\n",
        "\n",
        "#Standard Deviation:\n",
        "std_X = stdev(X)\n",
        "std_Y = stdev(Y)\n",
        "\n",
        "#Mean:\n",
        "mean_X = mean(X)\n",
        "mean_Y = mean(Y)\n",
        "\n",
        "#Print coefficient of varition:\n",
        "print(\"Coefficient of variation for X =\",std_X/mean_X)\n",
        "print(\"Coefficient of variation for Y =\",std_Y/mean_Y)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Coefficient of variation for X = 0.002701021298743379\n",
            "Coefficient of variation for Y = 0.008920960934171577\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ka7F7Zrel8ZQ",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "2b597343-989e-4c46-baff-678b70c13ee0"
      },
      "source": [
        "#Skewness for normally distributed data (Page 86):\n",
        "\n",
        "#Import required libraries:\n",
        "from statistics import mean,median,mode\n",
        "from scipy.stats import skew\n",
        "import numpy as np\n",
        "\n",
        "#Get normally distributed data:\n",
        "X = np.random.normal(0,2,10000)\n",
        "\n",
        "#Print the data:\n",
        "print(X,\"\\n\")\n",
        "\n",
        "#Print Mean:\n",
        "print(\"Mean =\",mean(X))\n",
        "\n",
        "#Print Median:\n",
        "print(\"Median = \",median(X))\n",
        "\n",
        "#Print Mode if available:\n",
        "try:\n",
        "    print(\"Mode = \",mode(X))\n",
        "except:\n",
        "    print(\"Mode not found!\")\n",
        "    \n",
        "#Find the skewness:\n",
        "print(\"Skewness =\",skew(X))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "[-1.67353217 -0.3733692   1.84257847 ... -0.92563567  1.23147463\n",
            "  3.59448025] \n",
            "\n",
            "Mean = 0.006601646204447767\n",
            "Median =  -0.00625347509571745\n",
            "Mode not found!\n",
            "Skewness = -0.012743116508763926\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "e1F_AXZdl9Wx",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "a963be67-447e-477d-d0a6-82dc85484b85"
      },
      "source": [
        "#Skewness for normally distributed data (Page 87):\n",
        "\n",
        "#Import required libraries:\n",
        "from statistics import mean,median,mode\n",
        "from scipy.stats import skew\n",
        "import numpy as np\n",
        "\n",
        "\n",
        "#Get normally distributed data:\n",
        "X = [1,2,3,4,5]\n",
        "\n",
        "#Print the data:\n",
        "print(X,\"\\n\")\n",
        "\n",
        "#Print Mean:\n",
        "print(\"Mean =\",mean(X))\n",
        "\n",
        "#Print Median:\n",
        "print(\"Median = \",median(X))\n",
        "\n",
        "#Print Mode if available:\n",
        "try:\n",
        "    print(\"Mode = \",mode(X))\n",
        "except:\n",
        "    print(\"Mode not found!\")\n",
        "    \n",
        "#Find the skewness:\n",
        "print(\"Skewness =\",skew(X))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "[1, 2, 3, 4, 5] \n",
            "\n",
            "Mean = 3\n",
            "Median =  3\n",
            "Mode not found!\n",
            "Skewness = 0.0\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "H-zDfThBl-W3",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "7e4de0c1-8de8-448c-aab7-5bfbbca4b235"
      },
      "source": [
        "#Moderately Positive Skewed Data (Page 88):\n",
        "\n",
        "#Import required libraries:\n",
        "from statistics import mean,median,mode\n",
        "from scipy.stats import skew\n",
        "import numpy as np\n",
        "\n",
        "\n",
        "#Get normally distributed data:\n",
        "X = [1,1,1,2,3,4,5]\n",
        "\n",
        "#Print the data:\n",
        "print(X,\"\\n\")\n",
        "\n",
        "#Print Mean:\n",
        "print(\"Mean =\",mean(X))\n",
        "\n",
        "#Print Median:\n",
        "print(\"Median = \",median(X))\n",
        "\n",
        "#Print Mode if available:\n",
        "try:\n",
        "    print(\"Mode = \",mode(X))\n",
        "except:\n",
        "    print(\"Mode not found!\")\n",
        "    \n",
        "#Find the skewness:\n",
        "print(\"Skewness =\",skew(X))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "[1, 1, 1, 2, 3, 4, 5] \n",
            "\n",
            "Mean = 2.4285714285714284\n",
            "Median =  2\n",
            "Mode =  1\n",
            "Skewness = 0.5200705032248689\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ehoE3RS-mATY",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "b8e384e1-2c20-4ed0-db0f-ee30aa4a1ece"
      },
      "source": [
        "#Moderately Negative Skewed Data (Page 89):\n",
        "\n",
        "#Import required libraries:\n",
        "from statistics import mean,median,mode\n",
        "from scipy.stats import skew\n",
        "import numpy as np\n",
        "\n",
        "\n",
        "#Get normally distributed data:\n",
        "X = [1,2,3,4,5,5,5]\n",
        "\n",
        "#Print the data:\n",
        "print(X,\"\\n\")\n",
        "\n",
        "#Print Mean:\n",
        "print(\"Mean =\",mean(X))\n",
        "\n",
        "#Print Median:\n",
        "print(\"Median = \",median(X))\n",
        "\n",
        "#Print Mode if available:\n",
        "try:\n",
        "    print(\"Mode = \",mode(X))\n",
        "except:\n",
        "    print(\"Mode not found!\")\n",
        "    \n",
        "#Find the skewness:\n",
        "print(\"Skewness =\",skew(X))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "[1, 2, 3, 4, 5, 5, 5] \n",
            "\n",
            "Mean = 3.5714285714285716\n",
            "Median =  4\n",
            "Mode =  5\n",
            "Skewness = -0.5200705032248687\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "qCQGamREl_Vz",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "d25862b0-0a49-4d2a-e60c-6d8ca69d688d"
      },
      "source": [
        "#Highly Positive Skewed Data (Page 90):\n",
        "\n",
        "#Import required libraries:\n",
        "from statistics import mean,median,mode\n",
        "from scipy.stats import skew\n",
        "import numpy as np\n",
        "\n",
        "\n",
        "#Get normally distributed data:\n",
        "X = [1,1,1,1,1,1,1,1,1,2,3,4,5]\n",
        "\n",
        "#Print the data:\n",
        "print(X,\"\\n\")\n",
        "\n",
        "#Print Mean:\n",
        "print(\"Mean =\",mean(X))\n",
        "\n",
        "#Print Median:\n",
        "print(\"Median = \",median(X))\n",
        "\n",
        "#Print Mode if available:\n",
        "try:\n",
        "    print(\"Mode = \",mode(X))\n",
        "except:\n",
        "    print(\"Mode not found!\")\n",
        "    \n",
        "#Find the skewness:\n",
        "print(\"Skewness =\",skew(X))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "[1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 5] \n",
            "\n",
            "Mean = 1.7692307692307692\n",
            "Median =  1\n",
            "Mode =  1\n",
            "Skewness = 1.4579260622434016\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "7jlo1easmBPv",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "f02647cb-e26e-462c-dcdc-0aaee700d730"
      },
      "source": [
        "#Highly Negative Skewed Data (Page 91):\n",
        "\n",
        "#Import required libraries:\n",
        "from statistics import mean,median,mode\n",
        "from scipy.stats import skew\n",
        "import numpy as np\n",
        "\n",
        "\n",
        "#Get normally distributed data:\n",
        "X = [1,2,3,4,5,5,5,5,5,5,5,5,5]\n",
        "\n",
        "#Print the data:\n",
        "print(X,\"\\n\")\n",
        "\n",
        "#Print Mean:\n",
        "print(\"Mean =\",mean(X))\n",
        "\n",
        "#Print Median:\n",
        "print(\"Median = \",median(X))\n",
        "\n",
        "#Print Mode if available:\n",
        "try:\n",
        "    print(\"Mode = \",mode(X))\n",
        "except:\n",
        "    print(\"Mode not found!\")\n",
        "    \n",
        "#Find the skewness:\n",
        "print(\"Skewness =\",skew(X))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "[1, 2, 3, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5] \n",
            "\n",
            "Mean = 4.230769230769231\n",
            "Median =  5\n",
            "Mode =  5\n",
            "Skewness = -1.4579260622434014\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "2cCThQ4zmCgQ",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "038a1abd-e7f4-43fb-acac-0e9ccae7e03f"
      },
      "source": [
        "#Mesokurtic (Page 95):\n",
        "#Fisher=False\n",
        "\n",
        "#Import required libraries:\n",
        "from scipy.stats import kurtosis\n",
        "import numpy as np\n",
        "\n",
        "#Generating normally distributed data:\n",
        "X=np.random.normal(0,1,100000)\n",
        "\n",
        "#Get the kurtosis value:\n",
        "print(\"Kurtosis = \",kurtosis(X,fisher=False))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Kurtosis =  2.9935711731099888\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "g7Ztf_C8mDkP",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "99784beb-0f67-4702-ff89-8e671bb17e71"
      },
      "source": [
        "#Mesokurtic (Page 95):\n",
        "#Fisher=True\n",
        "\n",
        "#Import required libraries:\n",
        "from scipy.stats import kurtosis\n",
        "import numpy as np\n",
        "\n",
        "#Generating normally distributed data:\n",
        "X=np.random.normal(0,1,100000)\n",
        "\n",
        "#Get the kurtosis value:\n",
        "print(\"Kurtosis = \",kurtosis(X,fisher=True))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Kurtosis =  -0.006764953701763332\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "1AYR11JpmElY",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "f9bc9b25-f077-4b97-dd8b-c64a8052c2b9"
      },
      "source": [
        "#Leptokurtic (Page 95):\n",
        "#Fisher=False\n",
        "\n",
        "#Import required libraries:\n",
        "from scipy.stats import kurtosis\n",
        "import numpy as np\n",
        "\n",
        "#Generating normally distributed data:\n",
        "X = [1,2,3,4,4,4,5,5,5,5,5,5,5,5,5]\n",
        "\n",
        "#Get the kurtosis value:\n",
        "print(\"Kurtosis = \",kurtosis(X,fisher=False))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Kurtosis =  4.007334183673471\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "t9HnMqn4mGI4",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "113d8b11-be6e-4e6a-da49-07c84bf726f0"
      },
      "source": [
        "#Leptokurtic (Page 96):\n",
        "#Fisher=True\n",
        "\n",
        "#Import required libraries:\n",
        "from scipy.stats import kurtosis\n",
        "import numpy as np\n",
        "\n",
        "#Generating normally distributed data:\n",
        "X = [1,2,3,4,4,4,5,5,5,5,5,5,5,5,5]\n",
        "\n",
        "#Get the kurtosis value:\n",
        "print(\"Kurtosis = \",kurtosis(X,fisher=True))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Kurtosis =  1.0073341836734713\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "XkCz5dKkmIQg",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "78832c6e-1e58-4f84-d114-025cd7062215"
      },
      "source": [
        "#Platykurtic (Page 96):\n",
        "#Fisher=False\n",
        "\n",
        "#Import required libraries:\n",
        "from scipy.stats import kurtosis\n",
        "import numpy as np\n",
        "\n",
        "#Generating normally distributed data:\n",
        "X = [1,2,3,4,5,6]\n",
        "\n",
        "#Get the kurtosis value:\n",
        "print(\"Kurtosis = \",kurtosis(X,fisher=False))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Kurtosis =  1.7314285714285718\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "XelZszBVmLxf",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "41d5b044-d6fe-443d-e09d-f55c95eeeb8b"
      },
      "source": [
        "#Platykurtic (Page 96):\n",
        "#Fisher=True\n",
        "\n",
        "#Import required libraries:\n",
        "from scipy.stats import kurtosis\n",
        "import numpy as np\n",
        "\n",
        "#Generating normally distributed data:\n",
        "X = [1,2,3,4,5,6]\n",
        "\n",
        "#Get the kurtosis value:\n",
        "print(\"Kurtosis = \",kurtosis(X,fisher=True))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Kurtosis =  -1.2685714285714282\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "mVSBDoyVmMyQ",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "adc9b8fd-1c70-4b8b-b3ac-aff7abda57af"
      },
      "source": [
        "#Covarience (Page 103):\n",
        "\n",
        "#Import required libraries:\n",
        "import numpy as np\n",
        "\n",
        "#Data:\n",
        "X = [100,103,110,115,119]\n",
        "Y = [1,7,15,20,22]\n",
        "\n",
        "#Print the covarience matrix:\n",
        "print(\"Covariance\\n\")\n",
        "print(np.cov(X,Y))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Covariance\n",
            "\n",
            "[[63.3 69.5]\n",
            " [69.5 78.5]]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "oR-WbuhLmO1V",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "c55dd811-ac73-4100-93b9-694d4cded6fd"
      },
      "source": [
        "#Correlation (Page 104):\n",
        "\n",
        "#Import required libraries:\n",
        "import numpy as np\n",
        "\n",
        "#Data:\n",
        "X = [100,103,110,115,119]\n",
        "Y = [1,7,15,20,22]\n",
        "\n",
        "#Print the correlation matrix:\n",
        "print(\"Correlation\\n\")\n",
        "print(np.corrcoef(X,Y))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Correlation\n",
            "\n",
            "[[1.         0.98593463]\n",
            " [0.98593463 1.        ]]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "aP98LIsIqyI0",
        "outputId": "4b3b8b82-aaa6-4ee9-aaff-a3c7c4ecf059"
      },
      "source": [
        "#1-Dimensional Data (Page 119):\r\n",
        "\r\n",
        "#Import required libraries:\r\n",
        "from scipy import stats\r\n",
        "\r\n",
        "#Dataset:\r\n",
        "d = [1,2,3,4,5]\r\n",
        "\r\n",
        "#Finding 0th moment:\r\n",
        "print(\"0th Moment = \",stats.moment(d,moment=0))\r\n",
        "\r\n",
        "#Finding 1st moment:\r\n",
        "print(\"1st Moment = \",stats.moment(d,moment=1))\r\n",
        "\r\n",
        "#Finding 2nd moment:\r\n",
        "print(\"2nd Moment = \",stats.moment(d,moment=2))\r\n",
        "\r\n",
        "#Finding 3nd moment:\r\n",
        "print(\"3nd Moment = \",stats.moment(d,moment=3))\r\n",
        "\r\n",
        "#Finding 4th moment:\r\n",
        "print(\"4th Moment = \",stats.moment(d,moment=4))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "0th Moment =  1.0\n",
            "1st Moment =  0.0\n",
            "2nd Moment =  2.0\n",
            "3nd Moment =  0.0\n",
            "4th Moment =  6.8\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "OYTishVaq-Q8",
        "outputId": "b6066842-dc61-4c8d-a6b7-170ab0912d47"
      },
      "source": [
        "#2-Dimensional Data (Page 119):\r\n",
        "\r\n",
        "#Import required libraries:\r\n",
        "from scipy import stats\r\n",
        "\r\n",
        "#Dataset:\r\n",
        "d = [[5,6,9,11,3],[21,4,8,15,2]]\r\n",
        "\r\n",
        "#Finding 0th moment:\r\n",
        "print(\"0th Moment = \",stats.moment(d,moment=0))\r\n",
        "\r\n",
        "#Finding 1st moment:\r\n",
        "print(\"1st Moment = \",stats.moment(d,moment=1))\r\n",
        "\r\n",
        "#Finding 2nd moment:\r\n",
        "print(\"2nd Moment = \",stats.moment(d,moment=2))\r\n",
        "\r\n",
        "#Finding 3nd moment:\r\n",
        "print(\"3nd Moment = \",stats.moment(d,moment=3))\r\n",
        "\r\n",
        "#Finding 4th moment:\r\n",
        "print(\"4th Moment = \",stats.moment(d,moment=4))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "0th Moment =  [1. 1. 1. 1. 1.]\n",
            "1st Moment =  [0. 0. 0. 0. 0.]\n",
            "2nd Moment =  [64.    1.    0.25  4.    0.25]\n",
            "3nd Moment =  [0. 0. 0. 0. 0.]\n",
            "4th Moment =  [4.096e+03 1.000e+00 6.250e-02 1.600e+01 6.250e-02]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "vBymVv03rB3D",
        "outputId": "0646a4ad-ed0d-432d-94a9-1d50acbbab52"
      },
      "source": [
        "#2-Dimensional Data (Page 120):\r\n",
        "#Set axis=1 (Horizonatal):\r\n",
        "\r\n",
        "#Import required libraries:\r\n",
        "from scipy import stats\r\n",
        "\r\n",
        "#Dataset:\r\n",
        "d = [[5,6,9,11,3],[21,4,8,15,2]]\r\n",
        "\r\n",
        "#Finding 0th moment:\r\n",
        "print(\"0th Moment = \",stats.moment(d,moment=0,axis=1))\r\n",
        "\r\n",
        "#Finding 1st moment:\r\n",
        "print(\"1st Moment = \",stats.moment(d,moment=1,axis=1))\r\n",
        "\r\n",
        "#Finding 2nd moment:\r\n",
        "print(\"2nd Moment = \",stats.moment(d,moment=2,axis=1))\r\n",
        "\r\n",
        "#Finding 3nd moment:\r\n",
        "print(\"3nd Moment = \",stats.moment(d,moment=3,axis=1))\r\n",
        "\r\n",
        "#Finding 4th moment:\r\n",
        "print(\"4th Moment = \",stats.moment(d,moment=4,axis=1))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "0th Moment =  [1. 1.]\n",
            "1st Moment =  [0. 0.]\n",
            "2nd Moment =  [ 8.16 50.  ]\n",
            "3nd Moment =  [  4.704 144.   ]\n",
            "4th Moment =  [ 110.8032 4134.8   ]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "zGrvyzh1rFoe",
        "outputId": "181ff1d6-c4dc-47dd-a2b5-fef92f706893"
      },
      "source": [
        "#Multi-Dimensional Data (Page 120):\r\n",
        "\r\n",
        "#Import required libraries:\r\n",
        "from scipy import stats\r\n",
        "\r\n",
        "#Dataset:\r\n",
        "d = [[5,6,9,11,3],\r\n",
        "     [21,4,8,15,2],\r\n",
        "    [15,23,42,1,36]]\r\n",
        "\r\n",
        "#Finding 0th moment:\r\n",
        "print(\"0th Moment = \",stats.moment(d,moment=0))\r\n",
        "\r\n",
        "#Finding 1st moment:\r\n",
        "print(\"1st Moment = \",stats.moment(d,moment=1))\r\n",
        "\r\n",
        "#Finding 2nd moment:\r\n",
        "print(\"2nd Moment = \",stats.moment(d,moment=2))\r\n",
        "\r\n",
        "#Finding 3nd moment:\r\n",
        "print(\"3nd Moment = \",stats.moment(d,moment=3))\r\n",
        "\r\n",
        "#Finding 4th moment:\r\n",
        "print(\"4th Moment = \",stats.moment(d,moment=4))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "0th Moment =  [1. 1. 1. 1. 1.]\n",
            "1st Moment =  [0. 0. 0. 0. 0.]\n",
            "2nd Moment =  [ 43.55555556  72.66666667 249.55555556  34.66666667 249.55555556]\n",
            "3nd Moment =  [ -84.74074074  420.         2779.25925926  -96.         2779.25925926]\n",
            "4th Moment =  [ 2845.62962963  7920.66666667 93416.96296296  1802.66666667\n",
            " 93416.96296296]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "206SuyPDrGvT",
        "outputId": "58612c7b-af34-4974-d5e1-b7e22679e237"
      },
      "source": [
        "#2-Dimensional Data (Page 121):\r\n",
        "#Set axis=1 (Horizonatal):\r\n",
        "#Higher Order Moments:\r\n",
        "\r\n",
        "#Import required libraries:\r\n",
        "from scipy import stats\r\n",
        "\r\n",
        "#Dataset:\r\n",
        "d = [[5,6,9,11,3],[21,4,8,15,2]]\r\n",
        "\r\n",
        "#Finding 10th moment:\r\n",
        "print(\"10th Moment = \",stats.moment(d,moment=10,axis=1))\r\n",
        "\r\n",
        "#Finding 12th moment:\r\n",
        "print(\"12th Moment = \",stats.moment(d,moment=12,axis=1))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "10th Moment =  [4.67770829e+05 5.41627985e+09]\n",
            "12th Moment =  [7.84184386e+06 6.41913756e+11]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ulGaelb9mQKX",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "26b568c2-863a-403f-c24d-6fb47bef0199"
      },
      "source": [
        "#Sample 1 (Page 131):\n",
        "\n",
        "#Import required libraries:\n",
        "from statistics import mean,stdev\n",
        "from scipy import stats\n",
        "\n",
        "#Data:\n",
        "X = [20,30,50,40,60]\n",
        "\n",
        "#Print Stats:\n",
        "print(\"Mean =\",mean(X))\n",
        "print(\"Standard Deviation =\",stdev(X))\n",
        "print(\"Standard Error From Mean =\",stats.sem(X))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Mean = 40\n",
            "Standard Deviation = 15.811388300841896\n",
            "Standard Error From Mean = 7.071067811865475\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "l9sXncXDmRGF",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "7c00bd69-1ab0-4bd1-a5c4-920dfbe158fe"
      },
      "source": [
        "#Sample 2 (Page 131):\n",
        "\n",
        "#Import required libraries:\n",
        "from statistics import mean,stdev\n",
        "from scipy import stats\n",
        "\n",
        "#Data:\n",
        "X = [50,60,100,20,10]\n",
        "\n",
        "#Print Stats:\n",
        "print(\"Mean =\",mean(X))\n",
        "print(\"Standard Deviation =\",stdev(X))\n",
        "print(\"Standard Error From Mean =\",stats.sem(X))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Mean = 48\n",
            "Standard Deviation = 35.63705936241092\n",
            "Standard Error From Mean = 15.937377450509226\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Crj_sg-xmR35",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "d0ca7952-aa80-45dc-82ff-881b8b6a5662"
      },
      "source": [
        "#Sample 3 (Page 132):\n",
        "\n",
        "#Import required libraries:\n",
        "from statistics import mean,stdev\n",
        "from scipy import stats\n",
        "\n",
        "#Data:\n",
        "X = [20,30,50,80,10]\n",
        "\n",
        "#Print Stats:\n",
        "print(\"Mean =\",mean(X))\n",
        "print(\"Standard Deviation =\",stdev(X))\n",
        "print(\"Standard Error From Mean =\",stats.sem(X))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Mean = 38\n",
            "Standard Deviation = 27.748873851023216\n",
            "Standard Error From Mean = 12.409673645990855\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "k1wEKC6VmTh3",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "06b1048a-3ecc-45ae-ba05-b21c4171da6c"
      },
      "source": [
        "#Confidence level = 68.3% (Page 135):\n",
        "\n",
        "#Import required libraries:\n",
        "from scipy import stats\n",
        "from statistics import mean\n",
        "\n",
        "#Data:\n",
        "X = [38,40,48]\n",
        "confidence_level = 0.683\n",
        "degree_free = len(X)-1\n",
        "sample_mean = mean(X)\n",
        "SEM = stats.sem(X)\n",
        "confidence_interval = stats.t.interval(confidence_level, degree_free, sample_mean, SEM)\n",
        "\n",
        "#Print the data:\n",
        "print(\"Confidence Interval =\",confidence_interval)                                                        "
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Confidence Interval = (37.959990366215784, 46.040009633784216)\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "l_JdzsBomSnh",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "39815d59-31a4-4c4d-ec5c-e50a60cfa329"
      },
      "source": [
        "#Confidence level = 95% (Page 136):\n",
        "\n",
        "#Import required libraries:\n",
        "from scipy import stats\n",
        "from statistics import mean\n",
        "\n",
        "#Data:\n",
        "X = [38,40,48]\n",
        "confidence_level = 0.95\n",
        "degree_free = len(X)-1\n",
        "sample_mean = mean(X)\n",
        "SEM = stats.sem(X)\n",
        "confidence_interval = stats.t.interval(confidence_level, degree_free, sample_mean, SEM)\n",
        "\n",
        "#Print the data:\n",
        "print(\"Confidence Interval =\",confidence_interval)                                                        "
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Confidence Interval = (28.855178784048796, 55.144821215951204)\n"
          ],
          "name": "stdout"
        }
      ]
    }
  ]
}